<template>
  <div class="app-container">
    <el-row :gutter="20" style="margin: 10px">
      <el-col :span="8" :xs="24">
        <el-card class="box-card">
          <template v-slot:header>
            <div class="clearfix">
              <span style="margin-right: 60px">患者信息</span>
              <el-button
                  type="primary"
                  plain
                  icon="search"
                  size="mini"
                  @click="handlePatientSelect"
                  style="float: right"
              >患者选择</el-button>
              
            </div>
          </template>
          <div>
            <el-form :model="formPatient" :rules="rulesPatient" ref="formPatient" :inline="true" label-width="80px">
              <el-form-item label="患者姓名" prop="name">
                <el-input
                    v-model="formPatient.name"
                    clearable
                    style="width: 240px"
                />
              </el-form-item>
              <el-form-item label="身份证号" prop="idNumber">
                <el-input
                    v-model="formPatient.idNumber"
                    clearable
                    style="width: 240px"
                />
              </el-form-item>
              <el-form-item label="电话号码" prop="phone">
                <el-input
                    v-model="formPatient.phone"
                    clearable
                    style="width: 240px"
                />
              </el-form-item>
               <el-form-item label="性别" prop="status">
              <el-select v-model="formPatient.gender" placeholder="请选择性别">
                <el-option label="男" value="M" />
                <el-option label="女" value="F" />
              </el-select>
            </el-form-item>
              <el-form-item label="年龄" prop="age">
                <el-input
                    v-model="formPatient.age"
                    clearable
                    style="width: 240px"
                />
              </el-form-item>

              <el-form-item label="地址" prop="address">
                <el-input
                    v-model="formPatient.address"
                    clearable
                    style="width: 240px"
                />
              </el-form-item>

              <el-form-item>
                <el-button type="primary" icon="Plus" @click="submitFormPatient">保存</el-button>
                <el-button icon="Refresh" @click="resetFormPatient">重置</el-button>
                <el-button type="success" @click="getListRecordnumber" style="width: 140px">获取最近病历编号</el-button>
              </el-form-item>
            </el-form>

          </div>
        </el-card>
      </el-col>
      <el-col :span="16" :xs="24">
        <el-card>

              <template v-slot:header>
                <div class="clearfix">
                  <span>填写病历表单&#12288;&#12288;&#12288;&#12288;&#12288;&#12288;&#12288;&#12288;病历编号</span>
                  <template v-if="currentRecordNumber">
                        <span  style="color: blue; margin: 0 20px;">{{currentRecordNumber}}</span>
                      </template>
                      
                  <el-button
                     style="float: right"
                      type="primary"
                      size="mini"
                      @click="submitForm"
                  >保存病历</el-button>
                </div>
              </template>

          <div>

           <!--左边卡片内容-->
            <el-form ref="form" :model="form" :rules="rulesrecord" label-width="120px">

                         <el-form-item label="患者编号" prop="patientId">
                          <el-input v-model="form.patientId" disabled />
                        </el-form-item>
                        <el-form-item label="医生编号" prop="doctorId" v-show="false">
                          <el-input v-model="form.doctorId" disabled />
                        </el-form-item>
                        <el-form-item label="主诉" prop="priDesc">
                          <el-input v-model="form.priDesc" type="textarea" placeholder="请输入主述"  />
                        </el-form-item>
                        <el-form-item label="现有病史">
                          <el-input v-model="form.presentIllness" type="textarea" placeholder="请输入现有病史"/>
                        </el-form-item>
                        <el-form-item label="现病史治疗情况">
                          <el-input v-model="form.presentMedicalCondition"   placeholder="请输入现病史治疗情况"/>
                        </el-form-item>
                        <el-form-item label="既往病史">
                          <el-input v-model="form.pmh" type="textarea" placeholder="请输入既往病史" />
                        </el-form-item>
                        <el-form-item label="过敏史">
                          <el-input v-model="form.allergy" type="textarea" placeholder="请输入过敏史"  />
                        </el-form-item>
                        <el-form-item label="体格检查">
                          <el-input v-model="form.healthCheckup" type="textarea" placeholder="请输入体格检查" />
                        </el-form-item>
                        <el-form-item label="检查建议">
                          <el-input v-model="form.checkAdvice" type="textarea" placeholder="请输入检查建议" />
                        </el-form-item>
                        <el-form-item label="注意事项">
                          <el-input v-model="form.attention" type="textarea" placeholder="请输入注意事项" />
                        </el-form-item>
                    </el-form>
                    
          </div>
        </el-card>
      </el-col>
    </el-row>

 <!--  选择患者对话框-->
 <el-dialog :title="titlePatient" v-model="openPatient" width="1000px" append-to-body>
      <el-form :model="queryParamsPatient" ref="queryParamsPatient" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="身份证" prop="number">
          <el-input
              v-model="queryParamsPatient.idNumber"
              placeholder="请输入身份证"
              clearable
              @keyup.enter.native="handleQueryPatient"
          />
        </el-form-item>
        <el-form-item label="患者姓名" prop="patientId">
          <el-input
              v-model="queryParamsPatient.name"
              placeholder="请输入患者姓名"
              clearable
              @keyup.enter.native="handleQueryPatient"
          />
        </el-form-item>
        <el-form-item label="患者电话" prop="doctorId">
          <el-input
              v-model="queryParamsPatient.phone"
              placeholder="请输入患者电话"
              clearable
              @keyup.enter.native="handleQueryPatient"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="search" size="mini" @click="handleQueryPatient">搜索</el-button>
          <el-button icon="Refresh" size="mini" @click="resetQueryPatient">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="patientList" highlight-current-row @current-change="handleCurrentChangePatient">
        <el-table-column type="index" label="#" width="50" />
        <el-table-column label="身份证号" align="center" prop="idNumber" />
        <el-table-column label="患者姓名" align="center" prop="name" />
        <el-table-column label="性别" align="center" prop="gender" />
        <el-table-column label="年龄" align="center" prop="age" />
        <el-table-column label="电话" align="center" prop="phone" />
        <el-table-column label="地址" align="center" prop="address" />
      </el-table>
      <pagination
          v-show="totalPatient>0"
          :total="totalPatient"
          v-model:page="queryParamsPatient.pageNum"
          v-model:limit="queryParamsPatient.pageSize"
          @pagination="getListPatient"
      />
    </el-dialog>
</div>

</template>

<script>
import { getUserProfile } from "@/api/system/user";
import { listPatient,addPatient, updatePatient } from "@/api/emr/patient";
import {ElMessage} from "element-plus";
import {listRecord,addRecord,updateRecord} from "@/api/emr/record";
import {reactive} from "vue";

export default {
  name: "doctor",
  data() {
    return {
      // 遮罩层
      loading: true,
      loading2: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      totalRecord: 0,
      // 确诊信息表格数据
      recordList: [],
      // 总条数
      totalPatient: 0,
      // 患者表格数据
      patientList: [],
      // 弹出层标题
      titlePatient: "",
      titleRecord: "",
      title: "",
      // 是否显示弹出层
      openPatient: false,
      openRecord: false,
      open: false,
      // 当前病人
      currentPatient: null,
      // 当前病历
      currentRecord: null,
      currentRecordNumber: null,
      // 查询参数
      queryParamsPatient: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        idNumber: null,
        phone: null,
        gender: null,
        age: null,

      },
      //病历表单参数
      queryParamsRecord: {
        pageNum: 1,
        pageSize: 10,
        patientId: null,
        medicalRecordId: null,
        doctorName: null,
        priDesc: null,
        presentIllness: null,
        presentMedicalCondition: null,
        pmh: null,
        allergy: null,
        healthCheckup: null,
        checkAdvice: null,
        attention: null,
        date: null,
      },
      // 患者表单参数
      formPatient: {},
      // 患者表单校验
      rulesPatient: {
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        idNumber: [
          { required: true, message: "身份证号不能为空且必须要为18位", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "电话号码不能为空且不能超过11位", trigger: "blur" }
        ],
      },
      // 表单参数
      form: {},
      // 表单校验
      rulesrecord: {
        priDesc:[
          {required: true, message: "主诉不能为空", trigger: "blur"}
        ],
        patientId:[
          {required: true, message: "未选择病人", trigger: "blur"} ]
      }

    };
  },
  created() {
    this.getListPatient();
  },
  methods: {
    /** 查询确诊信息列表 */
    getListPatient() {
      this.loading = true;
      listPatient(this.queryParamsPatient).then(response => {
        this.patientList = response.rows;
        this.totalPatient = response.total;
        this.loading = false;
      });
    },
    //获取新增的病历号
    getListRecordnumber() {
      this.queryParamsRecord.pageSize = 1000000000;
      this.loading2 = true;
      listRecord(this.queryParamsRecord).then(response=>{
       let ls=response.rows.length;
       this.currentRecordNumber=response.rows[ls-1].number;   
      
      });
      this.queryParamsRecord.pageSize = 10;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 病历表单重置
    reset() {
      this.form = {
        id: null,
        medicalRecordId: null,
        result: null,
        suggestion: null,
        attention: null,
        gender:null,
        date: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    // 患者搜索表单重置
    resetPatientQuery(){
      this.queryParamsPatient ={
        pageNum: 1,
        pageSize: 10,
        name: null,
        idNumber: null,
        phone: null,
        gender: null,
        age: null,
      };
      this.resetForm("queryParamsPatient")
    },
    /** 添加患者表单 */
    submitFormPatient() {
     
      this.$refs["formPatient"].validate(valid => {
        let pid=this.form.patientId;
        let s=this.formPatient.phone;
        let y=this.formPatient.idNumber;
       
              if ((s.length)<=11&&(y.length)==18)
              {
                if (valid) {
                if (this.formPatient.id != null) {
                  updatePatient(this.formPatient).then(response => {
                    ElMessage({
                      message: '修改成功',
                      type: 'success',
                    });
                  });
                } else {
                  addPatient(this.formPatient).then(response => {
                    ElMessage({
                      message: '新增成功',
                      type: 'success',
                    });
                  });
                }
                }
              }
              else{
                ElMessage({
                message: '电话号码长度不能超过11位或者身份证必须为18位',
                type: 'warning'
              });
              }
        

      });
    
    },

    /** 重置按钮操作 */
    resetFormPatient() {
      this.resetForm("formPatient");
      this.resetForm("form");
      this.formPatient.gender=null;
      this.currentRecordNumber = null;
    },

    /** 搜索按钮操作 */
    handleQueryPatient() {
      this.queryParamsPatient.pageNum = 1;
      this.getListPatient();
    },
    /** 重置按钮操作 */
    resetQueryPatient() {
      this.resetPatientQuery();

    },
    /** 重置按钮操作 */
    resetQueryRecord() {
      this.resetForm("queryParamsRecord");
      this.getListRecord();
    },
    // 患者单选选框选中数据
    handleCurrentChangePatient(selection) {
      let param = {
        idNumber: selection.idNumber,
      }
      listPatient(param).then(response => {
        this.formPatient = response.rows[0];
        this.form.patientId = this.formPatient.id;
        this.currentPatient = response.rows[0];
        this.openPatient = false;
      });
    },
    /** 选择患者按钮操作 */
    handlePatientSelect() {
      this.reset();
      this.openPatient = true;
      this.titlePatient = "选择患者";
      this.currentRecordNumber = null;
      this.getUser();
    },
    //获取当前用户
      getUser() {
        getUserProfile().then(response => {
          state.user = response.data;
          state.roleGroup = response.roleGroup;
          state.postGroup = response.postGroup;
          this.form.doctorId=state.user.userId;
        });
      },
    
      getNumber()
      {

      },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getListRecordnumber();
              
            });
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getListRecordnumber();
            });
          }
          
        }
      });
    },
  },

};
const state = reactive({
  user: {},
});
</script>
